/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: ticketQuery.c
> Author: KAIJIE.JIANG
> Mail: 
> Created Time: 2024年07月18日 星期四 02时28分42秒
***************************************************/

#include "ticketQuery.h"

//通过id查询票务信息
int queryIdToTicket(FILE *fp,TIC *head,int n){
	fp = fopen("./Ticket.txt","r");
	if(fp == NULL){
		perror("fopen Ticket!");
		exit(-1);
	}
	head = createTicketHead();
	head = ticketReadFile(fp,head);

	fclose(fp);

	TIC *cur = NULL;
	cur = head->next;
	int i = 0;
	int *point = NULL;
	while(cur != NULL){
		if(cur->data.id == n){
			printf("id:%d\torigin:%s\tfinish:%s\tdate:%s\tprice:%.2f\tleft:%.d\tgross:%d\n",\
					cur->data.id,cur->data.origin,cur->data.finish,cur->data.date,\
					cur->data.price,cur->data.left,cur->data.gross);
			point = cur->data.user;
			printf("乘客id如下：");
			for(i = 0; i < cur->data.gross; i++){
				if(i % 5 == 0){
					printf("\n");
				}
				printf("%d：%d\t",i,*(point+i));
			}
			printf("\n");
			break;
		}
		cur = cur->next;
	}

	if(cur == NULL){
		printf("未找到该票务！\n");
		n = 1;
	}

	freeTicketLink(head);
	return n;
}

//通过起点查询票务信息 返回值：id
int queryOriginToTicket(FILE *fp,TIC *head){
	int n = 0;
	char str[20] = {0};

	fp = fopen("./Ticket.txt","r");
	if(fp == NULL){
		perror("fopen Ticket!");
		exit(-1);
	}
	head = createTicketHead();
	head = ticketReadFile(fp,head);

	fclose(fp);

	TIC *cur = NULL;
	cur = head->next;
	int i = 0;
	int *point = NULL;
	printf("请输入需要查询的起点\n");
	printf("起点:");
	scanf("%s",str);

	while(cur != NULL){
		if(strcmp(cur->data.origin,str) == 0){
			n = cur->data.id;
			printf("id:%d\torigin:%s\tfinish:%s\tdate:%s\tprice:%.2f\tleft:%.d\tgross:%d\n",\
					cur->data.id,cur->data.origin,cur->data.finish,cur->data.date,\
					cur->data.price,cur->data.left,cur->data.gross);
			point = cur->data.user;
			printf("乘客id如下：");
			for(i = 0; i < cur->data.gross; i++){
				if(i % 5 == 0){
					printf("\n");
				}
				printf("%d：%d\t",i,*(point+i));
			}
			printf("\n");
			break;
		}
		cur = cur->next;
	}

	if(cur == NULL){
		printf("未找到该票务！\n");
		n = 1;
	}
	freeTicketLink(head);
	return n;
}

//通过终点查询票务信息 返回值：id
int queryFinishToTicket(FILE *fp,TIC *head){
	int n = 0;
	char str[20] = {0};

	fp = fopen("./Ticket.txt","r");
	if(fp == NULL){
		perror("fopen Ticket!");
		exit(-1);
	}
	head = createTicketHead();
	head = ticketReadFile(fp,head);

	fclose(fp);

	TIC *cur = NULL;
	cur = head->next;
	int i = 0;
	int *point = NULL;
	printf("请输入需要查询的终点\n");
	printf("终点:");
	scanf("%s",str);
	while(cur != NULL){
		if(strcmp(cur->data.finish,str) == 0){
			n = cur->data.id;
			printf("id:%d\torigin:%s\tfinish:%s\tdate:%s\tleft:%.d\tgross:%d\n",\
				cur->data.id,cur->data.origin,cur->data.finish,cur->data.date,\
				cur->data.left,cur->data.gross);
			point = cur->data.user;
			printf("乘客id如下：");
			for(i = 0; i < cur->data.gross; i++){
				if(i % 5 == 0){
					printf("\n");
				}
				printf("%d：%d\t",i,*(point+i));
			}
			printf("\n");
			break;
		}
		cur = cur->next;
	}

	if(cur == NULL){
		printf("未找到该票务！\n");
		n = 1;
	}

	freeTicketLink(head);
	return n;
}
